﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace AppCash
{
    public partial class frmReportDay : Form
    {



        #region ------窗体实例化------
        public frmReportDay()
        {
            InitializeComponent();
        }
        #endregion

        #region ------窗体加载------
        private void frmReportDay_Load(object sender, EventArgs e)
        {
            //设置DataGridView的显示样式
            gvList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            gvList.AutoGenerateColumns = false;

        }
        #endregion



        #region ------搜索------
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string strSql = "";
            string time1 = string.Empty;
            string time2 = string.Empty;
            string startTime = txtStart.Text.Trim();
            string endTime = txtEnd.Text.Trim();
            if (string.IsNullOrEmpty(startTime) && string.IsNullOrEmpty(endTime))
            {
                MessageBoxEx.Show("开始时间或结束时间不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtStart.Focus();
                return;
            }
            if (!string.IsNullOrEmpty(startTime) && !string.IsNullOrEmpty(endTime))
            {
                strSql += " and IDate >= #" + startTime + "# and IDate<=#" + endTime + "# ";
                time1 = startTime;
                time2 = endTime;
            }
            else
            {
                if (!string.IsNullOrEmpty(startTime))
                {
                    strSql += " and IDate = #" + startTime + "# ";
                    time1 = startTime;
                }
                else
                {
                    strSql += "  and IDate=#" + endTime + "# ";
                    time1 = endTime;
                }
            }
            fillGVList(strSql, time1, time2);
        }
        #endregion



        #region ------填出datagridview------
        private void fillGVList(string key, string time1, string time2)
        {

            ZhiKeDuoServer.BLL.SaleInfo bll = new ZhiKeDuoServer.BLL.SaleInfo();
            DataTable dt = new DataTable();
            dt = bll.getDayRePort(Convert.ToDateTime(time1), Convert.ToDateTime(time2));
            gvList.DataSource = dt;
            int count = 0;
            double price = 0;
            double gain = 0;
            foreach (DataRow dr in dt.Rows)
            {
                count += int.Parse(dr[0].ToString());
                price += double.Parse(dr[1].ToString());
                gain += double.Parse(dr[2].ToString());
            }
            lblCount.Text = count.ToString();
            lblXSZE.Text = price.ToString();
            lblGain.Text = gain.ToString();
        }




        #endregion

        private void buttonX1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sflg = new SaveFileDialog();
            sflg.Filter = "Excel(*.xls)|*.xls|Excel(*.xlsx)|*.xlsx";
            if (sflg.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }
            //this.gridView1.ExportToXls(sflg.FileName);
            //NPOI.xs book = new NPOI.HSSF.UserModel.HSSFWorkbook();
            NPOI.SS.UserModel.IWorkbook book = null;
            if (sflg.FilterIndex == 1)
            {
                book = new NPOI.HSSF.UserModel.HSSFWorkbook();
            }
            else
            {
                book = new NPOI.XSSF.UserModel.XSSFWorkbook();
            }

            NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("日营收");

            // 添加表头
            NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);
            int index = 0;
            foreach (DataGridViewColumn item in this.gvList.Columns)
            {
                if (item.Visible)
                {
                    NPOI.SS.UserModel.ICell cell = row.CreateCell(index);
                    cell.SetCellType(NPOI.SS.UserModel.CellType.String);
                    cell.SetCellValue(item.HeaderText);
                    index++;
                }
            }

            // 添加数据

            for (int i = 0; i < this.gvList.RowCount; i++)
            {
                index = 0;
                row = sheet.CreateRow(i + 1);
                foreach (DataGridViewColumn item in this.gvList.Columns)
                {
                    if (item.Visible)
                    {
                        NPOI.SS.UserModel.ICell cell = row.CreateCell(index);
                        cell.SetCellType(NPOI.SS.UserModel.CellType.String);
                        cell.SetCellValue(this.gvList.Rows[i].Cells[item.Index].ToString());
                        index++;
                    }
                }
            }
            // 写入 
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            book.Write(ms);
            book = null;

            using (FileStream fs = new FileStream(sflg.FileName, FileMode.Create, FileAccess.Write))
            {
                byte[] data = ms.ToArray();
                fs.Write(data, 0, data.Length);
                fs.Flush();
            }

            ms.Close();
            ms.Dispose();

        }

    }
}
